/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
 */

package com.xone.xui;

import org.apache.cordova.Config;
import org.apache.cordova.CordovaActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class XoneXui extends CordovaActivity {
	
	public final static String USER_AGENT_TAG = " ZHANGCHANG.CO.,LTD.";
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
        super.setIntegerProperty("splashscreen", R.drawable.splashscreen);
        super.appView.setBackgroundResource(R.drawable.splashscreen);
        String ua = super.appView.getSettings().getUserAgentString();
        super.appView.getSettings().setUserAgentString(ua + " VAPP/" + getSoftwareVersion() + USER_AGENT_TAG);
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html")
    }
    
	 /** 
     * Gets the software version retrieved from the Manifest. 
     */ 
	protected String getSoftwareVersion() {
		try {
			PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
			return packageInfo.versionName;
		} catch (PackageManager.NameNotFoundException e) {
			Log.e(TAG, "Package name not found", e);
		}
		return null;
	}
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, Menu.FIRST + 1, 0, "刷新");
		menu.add(Menu.NONE, Menu.FIRST + 10, 0, "退出");
		super.onCreateOptionsMenu(menu);
		return true;
	}
    
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
	        super.loadUrl(Config.getStartUrl());
			break;
		case Menu.FIRST + 10:
			confirmExit();
			break;
		}
		return true;
	}	
	
	public void confirmExit() {// 退出确认
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(XoneXui.this);
		alertDialog.setTitle("退出");
		alertDialog.setMessage("是否退出软件?");
		alertDialog.setPositiveButton("是", new DialogInterface.OnClickListener() {// 退出按钮
			@Override
			public void onClick(DialogInterface dialog, int i) {
				dialog.dismiss();
				finish();// 关闭activity
			}
		});
		alertDialog.setNegativeButton("否", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int i) {
				// 不退出不用执行任何操作
			}
		});
		alertDialog.show();// 显示对话框
	}
}

